package com.lswms.job;

import com.lswms.common.redis.RedisUtils;
import com.lswms.common.utils.MoneyUtil;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dto.YnDcProdDTO;
import com.lswms.dto.YnLotteryDTO;
import com.lswms.dto.YnSignDTO;
import com.lswms.dto.vo.resp.OrderDataListResp;
import com.lswms.entity.YnDcProdEntity;
import com.lswms.entity.YnLotteryEntity;
import com.lswms.entity.YnSignEntity;
import com.lswms.service.YnDcProdService;
import com.lswms.service.YnLotteryService;
import com.lswms.service.YnSignService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 
 * @author Administrator
 *
 */
@Component
@Slf4j
public class DcTasks {

    @Autowired
    private RedisUtils redisUtils;
    
    @Autowired
    private YnDcProdService ynDcProdService;

    /**
     * 申请人数（30s一次）
     */
    @Scheduled(cron="*/30 * * * * ?")
    public void prodApply(){
    	log.info("开始申请人数（30s一次）***");
    	Map<String, Object> params = new HashMap<String, Object>();
    	List<YnDcProdDTO> list = ynDcProdService.list(params);
    	
		Calendar calendar = Calendar.getInstance();
		int curHour24 = calendar.get(calendar.HOUR_OF_DAY);
		//每次增加量
		int zl = 0;
		if(curHour24 >= 0 && curHour24 < 5){
			zl = ThreadLocalRandom.current().nextInt(0, 2);
		}else if(curHour24 >= 6 && curHour24 < 22){
			zl = ThreadLocalRandom.current().nextInt(0, 5);
		}else if(curHour24 >= 22 && curHour24 < 24){
			zl = ThreadLocalRandom.current().nextInt(0, 2);
		}
    	for(YnDcProdDTO dto : list){
    		int count = Integer.parseInt(dto.getCount())+zl;
    		YnDcProdEntity entity = new YnDcProdEntity();
    		entity.setId(dto.getId());
    		entity.setCount(count+"");
    		entity.setUpdateDate(ZqDateUtil.getNow());
    		ynDcProdService.updateById(entity);
    	}
    	log.info("结束申请人数（30s一次）***");
    }
    /**
     * 累计升级人数、推荐人数（10s一次）
     */
    @Scheduled(cron="*/10 * * * * ?")
    public void totalUp(){
    	log.info("开始累计升级人数（10s一次）***");
    	//累计升级人数
    	Object obj = redisUtils.get("TotalUp");
    	if(obj == null){
    		redisUtils.set("TotalUp","5000");
    	}
    	Object count = redisUtils.get("TotalUp");
    	
    	String totalCount = (String)count;
    	
    	int zl = ThreadLocalRandom.current().nextInt(0, 5);
    	int zzcount = Integer.parseInt(totalCount) + zl;
    	redisUtils.set("TotalUp",zzcount+"");
    	
    	//今日累计推荐
    	String ymd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
    	Object obj1 = redisUtils.hGet("TotalRecommend:",ymd);
    	int counts = 0;
    	if(obj1 != null){
    		counts = Integer.parseInt((String)obj1);
    	}
    	int zl1 = ThreadLocalRandom.current().nextInt(0, 3);
    	counts = counts + zl1;
    	redisUtils.hSet("TotalRecommend:","ymd",counts+"");
    	log.info("结束累计升级人数（10s一次）***");
    }

}
